package com.example.weather;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class WeatherActivity extends Activity {
	
	private TextView weather,liveWeather,cityInfo,todayWeather,tomorrowWeather,aftertomorrowWeather,liveWeatherDetails,cityInfoDetails;
	private LinearLayout weatherLayout,weather_details_layout,city_info_layout;
	private ImageView todayMinImg,todayMaxImg,tomorrowMinImg,tomorrowMaxImg,aftertomorrowMinImg,aftertomorrowMaxImg;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.weather);
		
		initView();
		
		Intent intent = getIntent();
		String city = intent.getExtras().getString("city");
		WebServiceHelper wsh = new WebServiceHelper();
		Weather weather = wsh.getWeatherByCity(city);
		todayWeather.setText(((Map<String,Object>)weather.getList().get(0)).get("weatherTody").toString());
		tomorrowWeather.setText(((Map<String,Object>)weather.getList().get(1)).get("weatherTomorrow").toString());
		aftertomorrowWeather.setText(((Map<String,Object>)weather.getList().get(2)).get("weatherAfterTomorrow").toString());
		liveWeatherDetails.setText(weather.getLiveWeather());
		cityInfoDetails.setText(weather.getCityDescription());
		List icons = (List)((Map<String,Object>)weather.getList().get(0)).get("icons");
		todayMinImg.setImageResource(getResources().getIdentifier("a_" + icons.get(0) , "drawable", "com.example.weather"));
		todayMaxImg.setImageResource(getResources().getIdentifier("a_" + icons.get(1) , "drawable", "com.example.weather"));
		
		icons = (List)((Map<String,Object>)weather.getList().get(1)).get("icons");
		tomorrowMinImg.setImageResource(getResources().getIdentifier("a_" + icons.get(0) , "drawable", "com.example.weather"));
		tomorrowMaxImg.setImageResource(getResources().getIdentifier("a_" + icons.get(1) , "drawable", "com.example.weather"));
		
		icons = (List)((Map<String,Object>)weather.getList().get(2)).get("icons");
		aftertomorrowMinImg.setImageResource(getResources().getIdentifier("a_" + icons.get(0) , "drawable", "com.example.weather"));
		aftertomorrowMaxImg.setImageResource(getResources().getIdentifier("a_" + icons.get(1) , "drawable", "com.example.weather"));
	}
	
	private void initView(){
		todayWeather = (TextView)findViewById(R.id.today_weather);
		tomorrowWeather = (TextView)findViewById(R.id.tomorrow_weather);
		aftertomorrowWeather = (TextView)findViewById(R.id.after_tomorrow_weather);
		weather = (TextView)findViewById(R.id.weather);
		liveWeather = (TextView)findViewById(R.id.weather_details);
		cityInfo = (TextView)findViewById(R.id.city_info);
		liveWeatherDetails = (TextView)findViewById(R.id.liveweather_detail);
		cityInfoDetails = (TextView)findViewById(R.id.city_info_details);
		todayMinImg = (ImageView)findViewById(R.id.todayweather_img1);
		todayMaxImg = (ImageView)findViewById(R.id.todayweather_img2);
		tomorrowMinImg = (ImageView)findViewById(R.id.tomorrow_weather_img1);
		tomorrowMaxImg = (ImageView)findViewById(R.id.tomorrow_weather_img2);
		aftertomorrowMinImg = (ImageView)findViewById(R.id.after_tomorrow_weather_img1);
		aftertomorrowMaxImg = (ImageView)findViewById(R.id.after_tomorrow_weather_img2);
		
		weatherLayout = (LinearLayout)findViewById(R.id.weather_layout);
		weather_details_layout = (LinearLayout)findViewById(R.id.weather_details_layout);
		city_info_layout = (LinearLayout)findViewById(R.id.city_info_layout);
		
		weather.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				weather.setBackgroundColor(Color.parseColor("#00ff00"));
				liveWeather.setBackgroundColor(Color.parseColor("#ffffff"));
				cityInfo.setBackgroundColor(Color.parseColor("#ffffff"));
				weatherLayout.setVisibility(View.VISIBLE);
				weather_details_layout.setVisibility(View.INVISIBLE);
				city_info_layout.setVisibility(View.INVISIBLE);
			}
			
		});
		
		liveWeather.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				liveWeather.setBackgroundColor(Color.parseColor("#00ff00"));
				weather.setBackgroundColor(Color.parseColor("#ffffff"));
				cityInfo.setBackgroundColor(Color.parseColor("#ffffff"));
				weather_details_layout.setVisibility(View.VISIBLE);
				weatherLayout.setVisibility(View.INVISIBLE);
				city_info_layout.setVisibility(View.INVISIBLE);
			}
			
		});
		
		cityInfo.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				cityInfo.setBackgroundColor(Color.parseColor("#00ff00"));
				weather.setBackgroundColor(Color.parseColor("#ffffff"));
				liveWeather.setBackgroundColor(Color.parseColor("#ffffff"));
				city_info_layout.setVisibility(View.VISIBLE);
				weatherLayout.setVisibility(View.INVISIBLE);
				weather_details_layout.setVisibility(View.INVISIBLE);
			}
			
		});
	}
}
